package com.party.admin.biz.log;

import com.google.common.collect.Lists;
import com.party.admin.web.dto.input.common.CommonQuery;
import com.party.common.paging.Page;
import com.party.common.utils.DateUtils;
import com.party.core.model.log.LogEvent;
import com.party.core.model.log.LogEventQuery;
import com.party.core.service.log.ILogEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.party.common.utils.DateUtils.addMonth;

/**
 * 日志业务接口
 * Created by wei.li
 *
 * @date 2017/9/6 0006
 * @time 10:27
 */

@Service
public class LogEventBizService {

    @Autowired
    private ILogEventService logEventService;

    //时间参数键
    private final static String DAY = "day";

    //总数参数键
    private final static String COUNT = "count";

    /**
     * 日志列表
     * @param logEventQuery 日志
     * @param page 分页参数
     * @return 列表数据
     */
    public List<LogEvent> list(LogEventQuery logEventQuery, Page page){
        if (null != logEventQuery.getTimeType() && !new Integer(0).equals(logEventQuery.getTimeType())) {
            CommonQuery commonQuery = new CommonQuery();
            commonQuery.setTimeType(logEventQuery.getTimeType());
            logEventQuery.setCreateStart(commonQuery.getCreateStart());
            logEventQuery.setCreateEnd(commonQuery.getCreateEnd());
        }
        return logEventService.listPage(logEventQuery, page);
    }


    /**
     * 时间列表
     * @param length 长度
     * @return 时间
     */
    public List<Date> dateList(Integer length){
        length = null == length ? 7: length;
        Date now = new Date();
        Date startDate = null;
        if(length == 30){
            startDate = addMonth(DateUtils.getTodayDate(), -1);
        }else{
            startDate = DateUtils.addDay(now, -(length - 1));
        }
        List<Date> dateList =  DateUtils.getDateDifference(startDate, now);
        return dateList;
    }

    /**
     * 时间列表
     * @param length 长度
     * @return 时间
     */
    public List<String> dateStringList(Integer length){
        List<Date> list = dateList(length);
        List<String> dateList = Lists.newArrayList();
        for (Date date : list){
            dateList.add(DateUtils.formatDate(date));
        }
        return dateList;
    }


    /**
     * 视图列表
     * @param list 数据列表
     * @param dateList 时间列表
     * @return 视图列表
     */
    public List<Integer> chartList(List<HashMap<String, Object>> list, List<String> dateList){
        List<Integer> chartList = Lists.newArrayList();
        for (String date : dateList){
            Integer num = 0;
            for (Map<String, Object> map : list){
                String day = (String) map.get(DAY);
                Integer count = ((Long)map.get(COUNT)).intValue();
                if (day.equals(date)){
                    num = count;
                    break;
                }
            }
            chartList.add(num);
        }
        return chartList;
    }
}
